#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Created on 2011-11-25

主要用于从网站上爬取信息后，抽取页面信息；

@author: zhongfeng
'''

from pageparser import *
from spiderconfigparser import SpiderConfig

lusenRoot = ObuyUrlSummary(url=r'http://www.lusen.com/', name='lusen')

class LusenAllSortParser(RootCatagoryPageParser): 
    def __init__(self, dataStr, rootUrlSummary, include=None, exclude=None):
        super(LusenAllSortParser, self).__init__(dataStr, rootUrlSummary, include, exclude) 
                             
    def getBaseSort3UrlSums(self):       
        finalUrlList = []
        for t in self.soup.findAll(name='a',attrs={'class':'depth-1'}):#一级分类
            name,url = ParserUtils.parserTag_A(t)
            sort_3_urlsum = self.buildSort_N(url, name, self.rootUrlSummary,firstFinalPage=True)
            sort_3_urlsum.catagoryLevel = 3
            finalUrlList.append(sort_3_urlsum)
        return finalUrlList  
        
class LusenSort3PageParser(Sort3PageParser):
    '''
          三级页面解析类
    '''    
    def __init__(self, dataStr, rootUrlSummary, include=None, exclude=None):
        super(LusenSort3PageParser, self).__init__(dataStr, rootUrlSummary, include, exclude)
    
    def nextPageUrlPattern(self):
        pageSeg = '--0--{}--index.html'
        urlSeg = self.rootUrlSummary.url.rsplit('.',1)[0]
        return '%s%s' % (urlSeg,pageSeg)
    
    def getTotal(self):
        s = self.soup.find(name='span',attrs = {'class':'pageall'})
        if s is None:
            pageNum = 1
        else:
            pageNum = s.getText()
        totalPage =  int(pageNum)
        if totalPage > SpiderConfig.getMaxPage():
            totalPage = SpiderConfig.getMaxPage()
        return totalPage

    def __getSingleProdDetail(self, prod):
        pNameSeg = prod.find(attrs={'class':'goodinfo'})
        pName, url = ParserUtils.parserTag_A(pNameSeg.a)
        pid = prod['product']
        t = prod.find(name='td', attrs={'class':'price_button'})
        if t != None:
            currentPrice = ParserUtils.getPrice(t.getText())
        else:
            currentPrice = 0.00

        pastPrice = 0.00
        imgUrlSeg = prod.find(name='td', attrs={'class':'goodpic'})
        imgUrl = ParserUtils.getImgUrl(imgUrlSeg)
        prodDetail = ProductDetails(productId=pid, fullUrl=url, imageUrl=imgUrl, privPrice=currentPrice, pubPrice=pastPrice, 
            name=pName, adWords='')
        prodDetail.catagory = self.rootUrlSummary
        return prodDetail

    def parserPageInfos(self): 
        resultList = []     
        listSeg = self.soup.findAll(name='div',attrs={'id':re.compile(r'pdt-[0-9]*')})
        for prod in listSeg:
            prodDetail = self.__getSingleProdDetail(prod)
            resultList.append(prodDetail)
        return resultList

class LusenSort4PageParser(LusenSort3PageParser):
    '''
          分类四级页面为列表页面，只抽取Product信息
    '''
    def __init__(self, dataStr, rootUrlSummary, include=None, exclude=None):
        super(LusenSort4PageParser, self).__init__(dataStr, rootUrlSummary, include, exclude)
        
    def parserSubUrlSums(self):
        pass

parserDict = {0:LusenAllSortParser, 3:LusenSort3PageParser, 4:LusenSort4PageParser}
            
''' test '''
import os,chardet
curModDir = os.path.dirname(os.path.abspath(__file__))
testFilePath = os.path.join(curModDir,'test_resources')

from crawlerhttp import getContentFromUrlSum
def testAllSortPage():

    rootUrlSum = ObuyUrlSummary(url=r'http://www.lusen.com', name='lusen')
    content = getContentFromUrlSum(rootUrlSum)
    firstPage = LusenAllSortParser(content, rootUrlSum)
    for sort_3 in firstPage.parserSubUrlSums():       
        for index, urlsum in enumerate(sort_3.parentPath):
            print urlsum.name
        print sort_3.name,sort_3.url ,sort_3.catagoryLevel 

def testSort3Page():
    
    sort_3_urlsum = ObuyUrlSummary(url=r'http://www.lusen.com/gallery-175.html',
                                    parentPath=[('test')], catagoryLevel=3)
    
    content = getContentFromUrlSum(sort_3_urlsum)
    sort3Page = LusenSort3PageParser(content, sort_3_urlsum)
    for sort_4 in sort3Page.getSort4PageUrlSums():
        print sort_4.url
    
    for product in sort3Page.parserPageInfos():
        print product.name



if __name__ == '__main__':
    #testAllSortPage()
    testSort3Page()
    